import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/layout'
const _import = require('./asyncRouter')
Vue.use(VueRouter)
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login" */ '../views/login')
  },
  {
    path: '/',
    component: Layout,
    redirect: '/system',
    children: [{
      path: 'system',
      name: 'system',
      component: () => import('@/views/system/index'),
      meta: { title: 'system', icon: 'system' }
    }]
  },
  {
    path: '/table',
    component: Layout,
    name: 'table',
    redirect: '/table/table1/table1-1',
    meta: { title: '第一级', icon: 'ceshi' },
    children: [{
      path: 'table1',
      name: 'table1',
      component: () => import('@/views/table/index'),
      redirect: '/table1/table1-1',
      meta: { title: '第二级1', icon: 'system' },
      children: [{
        path: 'table1-1',
        name: 'table1-1',
        component: () => import('@/views/table/index'),
        meta: { title: '第三级1table', icon: 'ceshi' }
      }, {
        path: 'tree1-2',
        name: 'tree1-2',
        component: () => import('@/views/tree/index'),
        meta: { title: '第二级2', icon: 'ceshi' }
      }
      ]

    }, {
      path: 'table2',
      name: 'table2',
      meta: { title: '第二级2', icon: 'system' }
    }]
  }
]

const createRouter = () => {
  return new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
  })
}
const router = createRouter()
export function resetRouter () {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
// const asyncComponents = path => import('../views/')
export const changeComponents = (mnue = []) => {
  for (var i = 0; i < mnue.length; i++) {
    if (mnue[i].component) {
      if (mnue[i].component === 'Layout') {
        mnue[i].component = Layout
      } else {
        mnue[i].component = _import(mnue[i].component)
      }
    }
    if (mnue[i].children) {
      changeComponents(mnue[i].children)
    }
  }
  // mnue.forEach(item => {
  //   if (item.component) {
  //     item.component = () => import('@/views' + item.component)
  //   }
  //   if (item.children) {
  //     changeComponents(item.children)
  //   }
  // })
  return mnue
}
export default router
